﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace RichLabelSpace
{
    namespace Warp
    {
        public class RichString
        {
            private string m_text = "";
            private RichStringType m_string_type = RichStringType.SimpleLab;
            private RichLabelWarp m_warp = null;

            public RichString(RichLabelWarp warp)
            {
                this.m_warp = warp;
            }

            public string Text
            {
                get { return m_text; }
                private set { m_text = value; }
            }

            private RichStringType StringType
            {
                get { return m_string_type; }
                set { m_string_type = value; }
            }

            // 设置文字信息
            public void Disintegrate(string txt)
            {
                txt = txt.TrimStart(' ').TrimEnd(' ');
                if (string.IsNullOrEmpty(txt)) return;

                // 截取自己的那一串字符串
                // 如果是基础类型，就压栈处理,在尾部需要出栈操作
                m_string_type = RichStringTools.CheckType(txt);
                m_text = RichStringTools.SpliteRichStringByType(m_string_type, txt);
                if (string.IsNullOrEmpty(m_text)) return;

                var temp_txt = txt;
                var new_txt = RichStringTools.RemoveRichStringByType(m_string_type, txt, m_text);
                if (m_string_type == RichStringType.Lab || m_string_type == RichStringType.Url) DisintegrateUrlAndLabel(temp_txt, new_txt);
                else DisntegarateOtherSign(new_txt);
            }

            private void DisintegrateUrlAndLabel(string temp_txt, string new_txt)
            {
                var _my_txt = m_text;
                m_text = RichStringTools.ChangeLabAndUrlEndSign(m_text, m_string_type);

                // 接取自己的字符串，如果还剩下有字符串，则做分割
                var sign = RichStringTools.match_type_dict[m_string_type];

                // 完整的元素内部的数据
                var _whole_str = RichStringTools.SpliteWholeRichStringByType(m_string_type, temp_txt);
                if (string.IsNullOrEmpty(_whole_str))
                {
                    Debug.Log("error richlabel  text: " + temp_txt);
                    m_string_type = RichStringType.Lab;
                    _whole_str = RichStringTools.MakeLabelElement(temp_txt);
                    this.m_warp.SetText(_whole_str);
                    return;
                }

                var start_symbol = _my_txt.Replace(sign.EndSign, "");
                var end_symbol = _whole_str.Replace(start_symbol, "");
                var _c_txt = end_symbol.Remove(end_symbol.LastIndexOf(sign.EndSign), sign.EndSign.Length);
                if (!string.IsNullOrEmpty(_c_txt))
                {
                    this.m_warp.RichStringStack.Push(this);
                    this.m_warp.SetText(_c_txt);
                    this.m_warp.RichStringStack.Pop();

                    // 元素剩余的数据解析
                    new_txt = temp_txt.Remove(0, _whole_str.Length);
                    this.m_warp.SetText(new_txt);
                }
            }

            private void DisntegarateOtherSign(string new_txt)
            {
                // 如果不是基础类型，解析字符串属性
                var array = this.m_warp.RichStringStack.ToArray();
                // 栈变成数组的时候需要从高位开始变化解析
                for (var i = array.Length - 1; i >= 0; --i)
                {
                    var rs = array[i];
                    var _txt = RichStringTools.GetPureTxt(rs.Text, rs.StringType);
                    if (rs.StringType == RichStringType.Lab && (m_string_type == RichStringType.SimpleLab || m_string_type == RichStringType.Lab))
                    {
                        var property = RichStringTools.GetProperty(_txt);
                        if (m_string_type == RichStringType.SimpleLab)
                        {
                            var key = "text";
                            var val = "'" + RichTxtTool.ReplaceSpecialSymbol(m_text) + "'";
                            if (property.ContainsKey(key)) property[key] = val;
                            else property.Add(key, val);

                            // 改变类型
                            if (rs.StringType == RichStringType.Lab) m_string_type = RichStringType.Lab;
                            m_text = RichStringTools.MakePropertyToRichString(property, m_string_type);
                        }
                        else if (m_string_type == RichStringType.Lab)
                        {
                            var _my_txt = RichStringTools.GetPureTxt(m_text, m_string_type);
                            var _my_property = RichStringTools.GetProperty(_my_txt);
                            foreach (var item in property)
                            {
                                if (_my_property.ContainsKey(item.Key)) _my_property[item.Key] = item.Value;
                                else _my_property.Add(item.Key, item.Value);
                            }
                            m_text = RichStringTools.MakePropertyToRichString(_my_property, m_string_type);
                        }
                    }
                    else if (rs.StringType == RichStringType.Url)
                    {
                        // 如果是URL就直接嵌套到里面去
                        if(m_string_type == RichStringType.SimpleLab)
                        {
                            m_text = "<label> " + _txt + "| text = '" + m_text + "' />";
                            m_string_type = RichStringType.Lab;
                        }
                        else
                        {
                            var start_sign = RichStringTools.match_type_dict[m_string_type].StartSign;
                            var insert_index = start_sign.Length;
                            m_text = m_text.Insert(insert_index, _txt + "|");
                        }

                    }
                }

                //继续解析下一个元素
                this.m_warp.AddRichString(this);
                this.m_warp.SetText(new_txt);
            }
        }
    }
}